import { Component, OnInit, Input, OnDestroy } from '@angular/core'
import { Observable } from 'rxjs'
import { CurrencyService } from '@shared/services/currency.service'
import { ChargeBackDataService } from '../../../charge-back.data.service'
import { I18NService } from '@core'
import { numScale } from 'app/utils/helpers'
import { take } from 'rxjs/operators'

@Component({
  selector: 'app-charge-back-debit-operate',
  templateUrl: './debit.operate.component.html'
})
export class DebitOperateComponent implements OnInit, OnDestroy {
  @Input() amt: number
  @Input() operateRemark: string

  @Input() currency: string
  /** 金额 */
  @Input() transAmt: number
  decimal$: Observable<number>
  constructor(
    private currencyService: CurrencyService,
    public chargeBackDataService: ChargeBackDataService,
    private i18n: I18NService,
  ) {}

  ngOnInit() {
    this.decimal$ = this.currencyService.getDecimalByCode(this.currency).pipe(take(1))
    this.chargeBackDataService.operateBody.amt = this.transAmt
    this.parseAmt()
  }

  ngOnDestroy() {
    this.chargeBackDataService.resetOperateBody()
  }

  parseAmt(): void {
    this.decimal$.subscribe(val => {
      if (val === 2) {
        this.transAmt = numScale(this.transAmt, -2)
        this.chargeBackDataService.operateBody.amt = this.transAmt
      }
    })
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
